package com.changgou.backend.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.feign.BrandFeign;
import com.changgou.goods.pojo.Brand;
import com.changgou.util.FastDFSClient;
import com.changgou.util.FastDFSFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/wbrand")
public class BrandController {
    @Autowired
    private BrandFeign brandFeign;



    @GetMapping("/tobrand")
//    @ResponseBody
    public String toBrand(){
        System.out.println("...");
        return "brand";
    }
    /**
     * 跳转到添加页面
     */
    @GetMapping("/toAddPage")
    public String toAddPage(){
        System.out.println("...");
        return "addBrand";
    }

    /**
     * 跳转到更新页面
     * @return
     */
    @GetMapping("/toupdate")
    public String toUpdate(Integer id, Model model){
        System.out.println(id);
        model.addAttribute("id",id);
        System.out.println("更新中");
        return "updateBrand";
    }


    @GetMapping
    @ResponseBody
    public Result findAll(){
        List<Brand> brandList = brandFeign.findAll().getData();
        return new Result(true, StatusCode.OK,"查询所有品牌列表成功", brandList);
    }

    /**
     * 分页查询
     * @return
     */
    @PostMapping("/search")
    @ResponseBody
    public Result search(@RequestBody Map<String,String> map){
        String currentPage = map.get("currentPage");
        String pageSize = map.get("pageSize");
        map.remove("currentPage");
        map.remove("pageSize");
        Result<PageResult> pageResult = brandFeign.findPage(map, Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        return new Result(true,StatusCode.OK,"查询分页成功",pageResult);
    }



    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody Brand brand){
        brandFeign.add(brand);
        return new Result(true, StatusCode.OK,"添加成功");
    }

    @PutMapping("/{id}")
    @ResponseBody
    public Result update(@RequestBody Brand brand, @PathVariable("id") Integer id){
        brandFeign.update(brand,id);
        return new Result(true, StatusCode.OK,"更新成功");
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public Result delete(@PathVariable("id") Integer id){
        brandFeign.delete(id);
        return new Result(true, StatusCode.OK,"删除成功");
    }

    @PostMapping("/upload")
    @ResponseBody
    public Result uploadFile(MultipartFile file){
        try {
            //判断文件是否存在
            if (file==null){
                throw new RuntimeException("文件不存在");
            }
            //获取文件的完整名称
            String originalFilename = file.getOriginalFilename();
            if (StringUtils.isEmpty(originalFilename)){
                throw new RuntimeException("文件不存在");
            }
            //获取文件的扩展名称
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            //获取文件内容
            byte[] content = file.getBytes();
            //创建文件上传的封装实体类
            FastDFSFile fastDFSFile = new FastDFSFile(originalFilename,content,extName);
            //基于工具类进行文件上传
            String[] uploadResult = FastDFSClient.upload(fastDFSFile);

            //封装返回结果
            String url=FastDFSClient.getTrackerUrl()+uploadResult[0]+"/"+uploadResult[1];
            System.out.println(url);
            return new Result(true,StatusCode.OK,"文件上传成功",url);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR,"文件上传失败");
    }





}
